package com.transaction.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.transaction.config.PageBean;
import com.transaction.config.R;
import com.transaction.domain.SysEntrust;
import com.transaction.service.SysEntrustService;
import com.transaction.service.SysInstitutionService;
import com.transaction.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: TODO
 * @Author 卟哭！
 * @Date 2024/03/26  15:09
 * @Version 1.0
 */
@RestController
@RequestMapping("entrust")
public class EntrustController {
    @Autowired
    private SysEntrustService entrustService;
    @Autowired
    private SysInstitutionService institutionService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 根据用户id查询委托单
     * @param entrust
     * @param page
     * @return
     */
    @GetMapping("list")
    public R entrustList(SysEntrust entrust, PageBean page) {
        Long userId = JwtUtils.GetUserIdByToken(request);
        PageHelper.startPage(page);
        entrust.setUserId(userId);
        List<SysEntrust> list = entrustService.seleteEntrustByUserId(entrust);
        PageInfo<SysEntrust> sysEntrustPageInfo = new PageInfo<>(list);
        return R.successed(sysEntrustPageInfo);
    }

    /**
     * 撤单功能  （逻辑删除）
     * @param entrustId
     * @return
     */
    @DeleteMapping("/delete/{entrustId}")
    public R deleteEntrust(@PathVariable Long entrustId) {
        entrustService.removeById(entrustId);
        return R.successed();
    }

    /**
     * 买入/卖出 委托单
     * @param entrust
     * @return
     */
    @PostMapping("save")
    public R saveEntrust(@RequestBody SysEntrust entrust) {
        entrustService.save(entrust);
        return R.successed();
    }


}
